library(haven)
library(foreign)
library(car)
library(dplyr)

df <- read_dta("~/Downloads/Wave2.dta")

#### CONFIDENCE IN MILITARY ####

## Q11 - confidence in the military (by conditions)
df$Q11_m <- df$Q11
df$Q11_m[df$Q11 == 98] <- NA
df$Q11_mr <- car::recode(df$Q11_m, "1=1;2=0.67;3=0.33;4=0")
df$Q11_d <- car::recode(df$Q11_m, "1=1;2=1;3=0;4=0")
table(df$Q11, df$Q11_mr)

## Q12 - confidence in the military (general/dummy)
df$Q12_d <- df$Q12
df$Q12_d[df$Q12 == 98] <- NA
df$Q12_d[df$Q12 == 2] <- 0

### Q14-16 - confidence broken down
conf_vars <- c("Q14", "Q15", "Q16")

for(i in conf_vars) {
  df[[paste0(i, "_r")]] <- df[[i]]
  df[[paste0(i, "_r")]] <- car::recode(df[[paste0(i, "_r")]], 
                                       "1=1;2=0.67;3=0.33;4=0")
  df[[paste0(i, "_r")]][df[[i]] == 77 | df[[i]] == 98 |
                          df[[i]] == 99 | df[[i]] == 6] <- NA
}
# recoded variables set skipped/missing as NA, recode from 0-1 with increasing agreement

for(i in conf_vars) {
  df[[paste0(i, "_d")]] <- df[[i]]
  df[[paste0(i, "_d")]] <- car::recode(df[[paste0(i, "_r")]], 
                                       "1=1;0.67=1;0.33=0;0=0")
  df[[paste0(i, "_d")]][df[[i]] == 77 | df[[i]] == 98 |
                          df[[i]] == 99 | df[[i]] == 6] <- NA
}
# recoded variables set skipped/missing as NA, recode as dummy for agreement

### Military Attributes
ml_vars <- c("Q13A", "Q13B", "Q13C", "Q13D", "Q13E", "Q13F", "Q13G", "Q13H")

for(i in ml_vars) {
  df[[paste0(i, "_r")]] <- df[[i]]
  df[[paste0(i, "_r")]] <- car::recode(df[[paste0(i, "_r")]], 
                                       "1=1;2=0.75;3=0.5;4=0.25;5=0")
  df[[paste0(i, "_r")]][df[[i]] == 77 | df[[i]] == 98 |
                          df[[i]] == 99 | df[[i]] == 6] <- NA
}
# recoded variables set skipped/missing as NA, recode from 0-1 with increasing agreement

for(i in ml_vars) {
  df[[paste0(i, "_d")]] <- df[[i]]
  df[[paste0(i, "_d")]] <- car::recode(df[[paste0(i, "_r")]], 
                                       "1=1;0.75=1;0.5=0;0.25=0;5=0")
  df[[paste0(i, "_d")]][df[[i]] == 77 | df[[i]] == 98 |
                          df[[i]] == 99 | df[[i]] == 6] <- NA
}
# recoded variables set skipped/missing as NA, recode as dummy for agreement

#### INSTITUTIONS AND PROGRESSIONS COMPARISON ####

df$Q7_r <- df$Q7
df$Q7_r[df$Q7 == 98] <- NA
df$Q7_r <- car::recode(df$Q7_r, "1=1;2=0.67;3=0.33;4=0")
df$Q7_d <- car::recode(df$Q7_r, "1=1;0.67=1;0.33=0;0.33=0")
df$Q9_r <- df$Q9
df$Q9_r[df$Q9 == 98] <- NA
df$Q9_r <- car::recode(df$Q9_r, "1=1;2=0.67;3=0.33;4=0")
df$Q9_d <- car::recode(df$Q9_r, "1=1;0.67=1;0.33=0;0.33=0")

ip_vars <- c("Q8A", "Q8B", "Q8C", "Q8D", "Q8E", "Q8F", "Q8G", "Q8H",
             "Q10A", "Q10B", "Q10C", "Q10D", "Q10E", "Q10F", "Q10G", "Q10H")

for(i in ip_vars) {
  df[[paste0(i, "_r")]] <- df[[i]]
  df[[paste0(i, "_r")]] <- car::recode(df[[paste0(i, "_r")]], 
                                           "1=1;2=0.75;3=0.5;4=0.25;5=0")
  df[[paste0(i, "_r")]][df[[i]] == 77 | df[[i]] == 98 |
                          df[[i]] == 99 | df[[i]] == 6] <- NA
}
# recoded variables set skipped/missing as NA, recode from 0-1 with increasing agreement

for(i in ip_vars) {
  df[[paste0(i, "_d")]] <- df[[i]]
  df[[paste0(i, "_d")]] <- car::recode(df[[paste0(i, "_r")]], 
                                       "1=1;0.75=1;0.5=0;0.25=0;5=0")
  df[[paste0(i, "_d")]][df[[i]] == 77 | df[[i]] == 98 |
                          df[[i]] == 99 | df[[i]] == 6] <- NA
}
# recoded variables set skipped/missing as NA, recode as dummy for agreement

#### CUEING EXPERIMENTS ####

cue_vars <- c("Q21", "Q25", "Q25A", "Q27", "Q20DD")

for(i in cue_vars) {
  df[[paste0(i, "_r")]] <- df[[i]]
  df[[paste0(i, "_r")]] <- car::recode(df[[paste0(i, "_r")]], 
                                       "1=1;2=0.75;3=0.5;4=0.25;5=0")
  df[[paste0(i, "_r")]][df[[i]] == 77 | df[[i]] == 98 |
                          df[[i]] == 99 | df[[i]] == 6] <- NA
}
# recoded variables set skipped/missing as NA, recode from 0-1 with increasing agreement

for(i in cue_vars) {
  df[[paste0(i, "_d")]] <- df[[i]]
  df[[paste0(i, "_d")]] <- car::recode(df[[paste0(i, "_r")]], 
                                       "1=1;0.75=1;0.5=0;0.25=0;5=0")
  df[[paste0(i, "_d")]][df[[i]] == 77 | df[[i]] == 98 |
                          df[[i]] == 99 | df[[i]] == 6] <- NA
}
# recoded variables set skipped/missing as NA, recode as dummy for agreement

## Trump support - cueing experiment
df$Q36_trump <- NA
df$Q36_trump[df$Q36 < 5] <- 0
df$Q36_trump[df$Q36 == 2] <- 1

#### PLANNING, ADVICE, INTEGRATION (Q24A-F) ####

pai_vars <- c("Q24A", "Q24B", "Q24C", "Q24D", "Q24E", "Q24F")

for(i in pai_vars) {
  df[[paste0(i, "_r")]] <- df[[i]]
  df[[paste0(i, "_r")]] <- car::recode(df[[paste0(i, "_r")]], 
                                       "1=1;2=0.75;3=0.5;4=0.25;5=0")
  df[[paste0(i, "_r")]][df[[i]] == 77 | df[[i]] == 98 |
                          df[[i]] == 99 | df[[i]] == 6] <- NA
}
# recoded variables set skipped/missing as NA, recode from 0-1 with increasing agreement

for(i in pai_vars) {
  df[[paste0(i, "_d")]] <- df[[i]]
  df[[paste0(i, "_d")]] <- car::recode(df[[paste0(i, "_r")]], 
                                       "1=1;0.75=1;0.5=0;0.25=0;5=0")
  df[[paste0(i, "_d")]][df[[i]] == 77 | df[[i]] == 98 |
                          df[[i]] == 99 | df[[i]] == 6] <- NA
}
# recoded variables set skipped/missing as NA, recode as dummy for agreement

#### DEMOGRAPHICS ####

## Party
df$dem <- ifelse(df$PARTYID7 <= 3, 1, 0)
df$ind <- ifelse(df$PARTYID7 == 4, 1, 0)
df$rep <- ifelse(df$PARTYID7 >= 5 & df$PARTYID7 <= 7, 1, 0)
df$party <- NA
df$party[df$dem == 1] <- 0
df$party[df$PARTYID7 == 4] <- 1
df$party[df$rep == 1] <- 2

## Religion
df$catholic <- ifelse(df$RELIG == 2, 1, 0)
df$christian <- ifelse(df$RELIG == 1 | df$RELIG == 3 |
                        df$RELIG == 4 | df$RELIG == 12, 1, 0)
df$norelig <- ifelse(df$RELIG >= 9 & df$RELIG <= 11, 1, 0)
df$religion <- ifelse(df$christian == 1, 1, 0)
df$religion[df$catholic == 1] <- 2
df$religion[df$RELIG >= 5 & df$RELIG <=8 | 
              df$RELIG == 13 | df$RELIG == 14] <- 3
df$religion[df$norelig == 1] <- 4

## Ideology
df$ideo_m <- ifelse(df$IDEO == 8, NA, df$IDEO) # DK as NA
df$ideo_m <- ifelse(df$IDEO == 98, NA, df$ideo_m)
df$ideo_m[df$IDEO == 9] <- NA
df$ideo_r <- ifelse(df$IDEO == 8, 4, df$IDEO) # DK as moderate
df$ideo3 <- NA
df$ideo3[df$ideo_m <= 3] <- 0
df$ideo3[df$ideo_m == 4] <- 1
df$ideo3[df$ideo_m >= 5] <- 2

df$lib <- ifelse(df$ideo_m <= 3, 1, 0)
df$mod <- ifelse(df$ideo_m == 4, 1, 0)
df$con <- ifelse(df$ideo_m >= 5, 1, 0)

## Gender
df$male <- ifelse(df$GENDER == 1, 1, 0)

## Race
df$black <- ifelse(df$RACETHNICITY == 2, 1, 0)
df$hispanic <- ifelse(df$RACETHNICITY == 4, 1, 0)
df$asian <- ifelse(df$RACETHNICITY == 6, 1, 0)
df$white <- ifelse(df$RACETHNICITY == 1, 1, 0)
df$other_race <- ifelse(df$RACETHNICITY == 3 | df$RACETHNICITY == 5 |
                                   df$RACETHNICITY == 6, 1, 0)

## Income
df$income5 <- NA
df$income5[df$INCOME < 5] <- 1
df$income5[df$INCOME >= 5 & df$INCOME <= 8] <- 2
df$income5[df$INCOME >= 9 & df$INCOME <= 11] <- 3
df$income5[df$INCOME >= 12 & df$INCOME <= 14] <- 4
df$income5[df$INCOME >= 15] <- 5

## Generation
df$silent <- ifelse(df$AGE >= 74, 1, 0)
df$boomer <- ifelse(df$AGE >= 55 & df$AGE <= 73, 1, 0)
df$genx <- ifelse(df$AGE >= 39 & df$AGE <= 54, 1, 0)
df$millen <- ifelse(df$AGE >= 23 & df$AGE <= 38, 1, 0)
df$generation <- NA
df$generation[df$AGE >= 74 & df$AGE <= 100] <- 1 # silent
df$generation[df$AGE >= 55 & df$AGE <= 73] <- 2 # boomer
df$generation[df$AGE >= 39 & df$AGE <= 54] <- 3 # gen x
df$generation[df$AGE >= 23 & df$AGE <= 38] <- 4 # millennial
df$generation[df$AGE <= 22] <- 5 # gen z

## Veteran Status
df$vet <- ifelse(df$VETERAN == 1 & df$VETERAN2 == 2, 1, 0)
df$vet[df$VETERAN == 98 | df$VETERAN2 == 98] <- NA
df$activeduty <- ifelse(df$VETERAN == 1 & df$VETERAN2 == 1, 1, 0)
df$activeduty[df$VETERAN == 98 | df$VETERAN2 == 98] <- NA

df$vetstatus <- NA
df$vetstatus[df$VETERAN == 1 & df$VETERAN2 == 1] <- 1
df$vetstatus[df$VETERAN == 1 & df$VETERAN2 == 2] <- 2
df$vetstatus[df$VETERAN == 2] <- 3

df$social <- ifelse(df$Q38 == 1, 1, 0)
df$social[df$Q38 == 98] <- NA
df$family <- ifelse(df$Q39 == 1, 1, 0)
df$family[df$Q39 == 77 | df$Q39 == 98] <- NA

## Region
df$midwest <- ifelse(df$REGION4 == 2, 1, 0)
df$south <- ifelse(df$REGION4 == 3, 1, 0)
df$west <- ifelse(df$REGION4 == 4, 1, 0)

df$city <- ifelse(df$URBAN3 == 1, 1, 0)
df$rural <- ifelse(df$URBAN3 == 3, 1, 0)

## Employment
df$unemployed <- ifelse(df$EMPLOY <= 2 | df$EMPLOY == 5, 0, 1)

## Education 
df$edulvl <- NA
df$edulvl[df$EDUC <= 9] <- 1
df$edulvl[df$EDUC >= 10 & df$EDUC <= 11] <- 2
df$edulvl[df$EDUC == 12] <- 3
df$edulvl[df$EDUC >= 13] <- 4

## Marital Status
df$married <- ifelse(df$MARITAL == 1, 1, 0)

#### MAIN TREATMENT DUMMIES ####

df$A1 <- ifelse(df$P_ASSIGN1 == 1, 1, 0)
df$A2 <- ifelse(df$P_ASSIGN1 == 2, 1, 0)
df$A3 <- ifelse(df$P_ASSIGN1 == 3, 1, 0)
df$A4 <- ifelse(df$P_ASSIGN1 == 4, 1, 0)
df$A5 <- ifelse(df$P_ASSIGN1 == 5, 1, 0)
df$A6 <- ifelse(df$P_ASSIGN1 == 6, 1, 0)
df$A7 <- ifelse(df$P_ASSIGN1 == 7, 1, 0)
df$A8 <- ifelse(df$P_ASSIGN1 == 8, 1, 0)

#### TREATMENT SUBSETS - P_ASSIGN1, PARTY ####

df$A1_2 <- NA
df$A1_2[df$P_ASSIGN1 == 1] <- 0
df$A1_2[df$P_ASSIGN1 == 2] <- 1
df$A1_3 <- NA
df$A1_3[df$P_ASSIGN1 == 1] <- 0
df$A1_3[df$P_ASSIGN1 == 3] <- 1

df$A1_2_rep <- NA
df$A1_2_rep[df$P_ASSIGN1 == 1 & df$party == 2] <- 0
df$A1_2_rep[df$P_ASSIGN1 == 2 & df$party == 2] <- 1

df$A1_2_ind <- NA
df$A1_2_ind[df$P_ASSIGN1 == 1 & df$party == 1] <- 0
df$A1_2_ind[df$P_ASSIGN1 == 2 & df$party == 1] <- 1

df$A1_2_dem <- NA
df$A1_2_dem[df$P_ASSIGN1 == 1 & df$party == 0] <- 0
df$A1_2_dem[df$P_ASSIGN1 == 2 & df$party == 0] <- 1

df$A1_3_rep <- NA
df$A1_3_rep[df$P_ASSIGN1 == 1 & df$party == 2] <- 0
df$A1_3_rep[df$P_ASSIGN1 == 3 & df$party == 2] <- 1

df$A1_3_ind <- NA
df$A1_3_ind[df$P_ASSIGN1 == 1 & df$party == 1] <- 0
df$A1_3_ind[df$P_ASSIGN1 == 3 & df$party == 1] <- 1

df$A1_3_dem <- NA
df$A1_3_dem[df$P_ASSIGN1 == 1 & df$party == 0] <- 0
df$A1_3_dem[df$P_ASSIGN1 == 3 & df$party == 0] <- 1

df$A1_4 <- NA
df$A1_4[df$P_ASSIGN1 == 1] <- 0
df$A1_4[df$P_ASSIGN1 == 4] <- 1
df$A1_5 <- NA
df$A1_5[df$P_ASSIGN1 == 1] <- 0
df$A1_5[df$P_ASSIGN1 == 5] <- 1

df$A1_4_rep <- NA
df$A1_4_rep[df$P_ASSIGN1 == 1 & df$party == 2] <- 0
df$A1_4_rep[df$P_ASSIGN1 == 4 & df$party == 2] <- 1

df$A1_4_ind <- NA
df$A1_4_ind[df$P_ASSIGN1 == 1 & df$party == 1] <- 0
df$A1_4_ind[df$P_ASSIGN1 == 4 & df$party == 1] <- 1

df$A1_4_dem <- NA
df$A1_4_dem[df$P_ASSIGN1 == 1 & df$party == 0] <- 0
df$A1_4_dem[df$P_ASSIGN1 == 4 & df$party == 0] <- 1

df$A1_5_rep <- NA
df$A1_5_rep[df$P_ASSIGN1 == 1 & df$party == 2] <- 0
df$A1_5_rep[df$P_ASSIGN1 == 5 & df$party == 2] <- 1

df$A1_5_ind <- NA
df$A1_5_ind[df$P_ASSIGN1 == 1 & df$party == 1] <- 0
df$A1_5_ind[df$P_ASSIGN1 == 5 & df$party == 1] <- 1

df$A1_5_dem <- NA
df$A1_5_dem[df$P_ASSIGN1 == 1 & df$party == 0] <- 0
df$A1_5_dem[df$P_ASSIGN1 == 5 & df$party == 0] <- 1

df$A1_6 <- NA
df$A1_6[df$P_ASSIGN1 == 1] <- 0
df$A1_6[df$P_ASSIGN1 == 6] <- 1
df$A1_7 <- NA
df$A1_7[df$P_ASSIGN1 == 1] <- 0
df$A1_7[df$P_ASSIGN1 == 7] <- 1
df$A1_8 <- NA
df$A1_8[df$P_ASSIGN1 == 1] <- 0
df$A1_8[df$P_ASSIGN1 == 8] <- 1

df$A1_6_rep <- NA
df$A1_6_rep[df$P_ASSIGN1 == 1 & df$party == 2] <- 0
df$A1_6_rep[df$P_ASSIGN1 == 6 & df$party == 2] <- 1

df$A1_6_ind <- NA
df$A1_6_ind[df$P_ASSIGN1 == 1 & df$party == 1] <- 0
df$A1_6_ind[df$P_ASSIGN1 == 6 & df$party == 1] <- 1

df$A1_6_dem <- NA
df$A1_6_dem[df$P_ASSIGN1 == 1 & df$party == 0] <- 0
df$A1_6_dem[df$P_ASSIGN1 == 6 & df$party == 0] <- 1

df$A1_7_rep <- NA
df$A1_7_rep[df$P_ASSIGN1 == 1 & df$party == 2] <- 0
df$A1_7_rep[df$P_ASSIGN1 == 7 & df$party == 2] <- 1

df$A1_7_ind <- NA
df$A1_7_ind[df$P_ASSIGN1 == 1 & df$party == 1] <- 0
df$A1_7_ind[df$P_ASSIGN1 == 7 & df$party == 1] <- 1

df$A1_7_dem <- NA
df$A1_7_dem[df$P_ASSIGN1 == 1 & df$party == 0] <- 0
df$A1_7_dem[df$P_ASSIGN1 == 7 & df$party == 0] <- 1

df$A1_8_rep <- NA
df$A1_8_rep[df$P_ASSIGN1 == 1 & df$party == 2] <- 0
df$A1_8_rep[df$P_ASSIGN1 == 8 & df$party == 2] <- 1

df$A1_8_ind <- NA
df$A1_8_ind[df$P_ASSIGN1 == 1 & df$party == 1] <- 0
df$A1_8_ind[df$P_ASSIGN1 == 8 & df$party == 1] <- 1

df$A1_8_dem <- NA
df$A1_8_dem[df$P_ASSIGN1 == 1 & df$party == 0] <- 0
df$A1_8_dem[df$P_ASSIGN1 == 8 & df$party == 0] <- 1

#### TREATMENT SUBSETS - P_ASSIGN2 (IRAN) ####

df$A2_support <- NA
df$A2_support[df$P_ASSIGN2 == 1] <- 0
df$A2_support[df$P_ASSIGN2 == 4] <- 1

df$A2_support_n <- NA
df$A2_support_n[df$P_ASSIGN2 == 1 & df$Q12_d == 0] <- 0
df$A2_support_n[df$P_ASSIGN2 == 4 & df$Q12_d == 0] <- 1
df$A2_support_c <- NA
df$A2_support_c[df$P_ASSIGN2 == 1 & df$Q12_d == 1] <- 0
df$A2_support_c[df$P_ASSIGN2 == 4 & df$Q12_d == 1] <- 1

df$A2_oppose <- NA
df$A2_oppose[df$P_ASSIGN2 == 1] <- 0
df$A2_oppose[df$P_ASSIGN2 == 5] <- 1

df$A2_oppose_n <- NA
df$A2_oppose_n[df$P_ASSIGN2 == 1 & df$Q12_d == 0] <- 0
df$A2_oppose_n[df$P_ASSIGN2 == 5 & df$Q12_d == 0] <- 1
df$A2_oppose_c <- NA
df$A2_oppose_c[df$P_ASSIGN2 == 1 & df$Q12_d == 1] <- 0
df$A2_oppose_c[df$P_ASSIGN2 == 5 & df$Q12_d == 1] <- 1

#### TREATMENT SUBSETS - P_ASSIGN3 (TRANSGENDER BAN) ####

df$A3_support <- NA
df$A3_support[df$P_ASSIGN3 == 1] <- 0
df$A3_support[df$P_ASSIGN3 == 3] <- 1

df$A3_support_n <- NA
df$A3_support_n[df$P_ASSIGN3 == 1 & df$Q12_d == 0] <- 0
df$A3_support_n[df$P_ASSIGN3 == 3 & df$Q12_d == 0] <- 1
df$A3_support_c <- NA
df$A3_support_c[df$P_ASSIGN3 == 1 & df$Q12_d == 1] <- 0
df$A3_support_c[df$P_ASSIGN3 == 3 & df$Q12_d == 1] <- 1

df$A3_support_n_dem <- ifelse(df$party == 0, df$A3_support_n, NA)
df$A3_support_n_rep <- ifelse(df$party == 2, df$A3_support_n, NA)
df$A3_support_c_dem <- ifelse(df$party == 0, df$A3_support_c, NA)
df$A3_support_c_rep <- ifelse(df$party == 2, df$A3_support_c, NA)

df$A3_oppose <- NA
df$A3_oppose[df$P_ASSIGN3 == 1] <- 0
df$A3_oppose[df$P_ASSIGN3 == 2] <- 1

df$A3_oppose_n <- NA
df$A3_oppose_n[df$P_ASSIGN3 == 1 & df$Q12_d == 0] <- 0
df$A3_oppose_n[df$P_ASSIGN3 == 2 & df$Q12_d == 0] <- 1
df$A3_oppose_c <- NA
df$A3_oppose_c[df$P_ASSIGN3 == 1 & df$Q12_d == 1] <- 0
df$A3_oppose_c[df$P_ASSIGN3 == 2 & df$Q12_d == 1] <- 1

df$A3_oppose_n_dem <- ifelse(df$party == 0, df$A3_oppose_n, NA)
df$A3_oppose_n_rep <- ifelse(df$party == 2, df$A3_oppose_n, NA)
df$A3_oppose_c_dem <- ifelse(df$party == 0, df$A3_oppose_c, NA)
df$A3_oppose_c_rep <- ifelse(df$party == 2, df$A3_oppose_c, NA)

df$A3_divide <- NA
df$A3_divide[df$P_ASSIGN3 == 1] <- 0
df$A3_divide[df$P_ASSIGN3 == 4] <- 1

df$A3_divide_n <- NA
df$A3_divide_n[df$P_ASSIGN3 == 1 & df$Q12_d == 0] <- 0
df$A3_divide_n[df$P_ASSIGN3 == 4 & df$Q12_d == 0] <- 1
df$A3_divide_c <- NA
df$A3_divide_c[df$P_ASSIGN3 == 1 & df$Q12_d == 1] <- 0
df$A3_divide_c[df$P_ASSIGN3 == 4 & df$Q12_d == 1] <- 1

#### TREATMENT SUBSETS - P_ASSIGN4 (GUNS IN SCHOOLS) ####

df$A4_more <- NA
df$A4_more[df$P_ASSIGN4 == 1] <- 0
df$A4_more[df$P_ASSIGN4 == 2] <- 1

df$A4_less <- NA
df$A4_less[df$P_ASSIGN4 == 1] <- 0
df$A4_less[df$P_ASSIGN4 == 3] <- 1

df$A4_divd <- NA
df$A4_divd[df$P_ASSIGN4 == 1] <- 0
df$A4_divd[df$P_ASSIGN4 == 4] <- 1

#### TREATMENT SUBSETS - P_ASSIGN5, PARTY ####

df$A5_2 <- NA
df$A5_2[df$P_ASSIGN5 == 1] <- 0
df$A5_2[df$P_ASSIGN5 == 2] <- 1
df$A5_3 <- NA
df$A5_3[df$P_ASSIGN5 == 1] <- 0
df$A5_3[df$P_ASSIGN5 == 3] <- 1
df$A5_4 <- NA
df$A5_4[df$P_ASSIGN5 == 1] <- 0
df$A5_4[df$P_ASSIGN5 == 4] <- 1
df$A5_5 <- NA
df$A5_5[df$P_ASSIGN5 == 1] <- 0
df$A5_5[df$P_ASSIGN5 == 5] <- 1

df$A5_2_rep <- NA
df$A5_2_rep[df$P_ASSIGN5 == 1 & df$party == 2] <- 0
df$A5_2_rep[df$P_ASSIGN5 == 2 & df$party == 2] <- 1
df$A5_2_ind <- NA
df$A5_2_ind[df$P_ASSIGN5 == 1 & df$party == 1] <- 0
df$A5_2_ind[df$P_ASSIGN5 == 2 & df$party == 1] <- 1
df$A5_2_dem <- NA
df$A5_2_dem[df$P_ASSIGN5 == 1 & df$party == 0] <- 0
df$A5_2_dem[df$P_ASSIGN5 == 2 & df$party == 0] <- 1

df$A5_3_rep <- NA
df$A5_3_rep[df$P_ASSIGN5 == 1 & df$party == 2] <- 0
df$A5_3_rep[df$P_ASSIGN5 == 3 & df$party == 2] <- 1
df$A5_3_ind <- NA
df$A5_3_ind[df$P_ASSIGN5 == 1 & df$party == 1] <- 0
df$A5_3_ind[df$P_ASSIGN5 == 3 & df$party == 1] <- 1
df$A5_3_dem <- NA
df$A5_3_dem[df$P_ASSIGN5 == 1 & df$party == 0] <- 0
df$A5_3_dem[df$P_ASSIGN5 == 3 & df$party == 0] <- 1

df$A5_4_rep <- NA
df$A5_4_rep[df$P_ASSIGN5 == 1 & df$party == 2] <- 0
df$A5_4_rep[df$P_ASSIGN5 == 4 & df$party == 2] <- 1
df$A5_4_ind <- NA
df$A5_4_ind[df$P_ASSIGN5 == 1 & df$party == 1] <- 0
df$A5_4_ind[df$P_ASSIGN5 == 4 & df$party == 1] <- 1
df$A5_4_dem <- NA
df$A5_4_dem[df$P_ASSIGN5 == 1 & df$party == 0] <- 0
df$A5_4_dem[df$P_ASSIGN5 == 4 & df$party == 0] <- 1

df$A5_5_rep <- NA
df$A5_5_rep[df$P_ASSIGN5 == 1 & df$party == 2] <- 0
df$A5_5_rep[df$P_ASSIGN5 == 5 & df$party == 2] <- 1
df$A5_5_ind <- NA
df$A5_5_ind[df$P_ASSIGN5 == 1 & df$party == 1] <- 0
df$A5_5_ind[df$P_ASSIGN5 == 5 & df$party == 1] <- 1
df$A5_5_dem <- NA
df$A5_5_dem[df$P_ASSIGN5 == 1 & df$party == 0] <- 0
df$A5_5_dem[df$P_ASSIGN5 == 5 & df$party == 0] <- 1

#### TREATMENT SUBSETS - P_ASSIGN3A, PARTY ####

df$A3A_2_rep <- NA
df$A3A_2_rep[df$P_ASSIGN3A == 1 & df$party == 2] <- 0
df$A3A_2_rep[df$P_ASSIGN3A == 2 & df$party == 2] <- 1
df$A3A_2_dem <- NA
df$A3A_2_dem[df$P_ASSIGN3A == 1 & df$party == 0] <- 0
df$A3A_2_dem[df$P_ASSIGN3A == 2 & df$party == 0] <- 1

df$A3A_3_rep <- NA
df$A3A_3_rep[df$P_ASSIGN3A == 1 & df$party == 2] <- 0
df$A3A_3_rep[df$P_ASSIGN3A == 3 & df$party == 2] <- 1
df$A3A_3_dem <- NA
df$A3A_3_dem[df$P_ASSIGN3A == 1 & df$party == 0] <- 0
df$A3A_3_dem[df$P_ASSIGN3A == 3 & df$party == 0] <- 1

#### NEGATIVE PARTISANSHIP ####

df$A1_copartisan <- NA
df$A1_copartisan[df$P_ASSIGN1 == 1] <- 0 # control
df$A1_copartisan[df$P_ASSIGN1 == 6 & df$party == 2] <- 1 # co-partisan
df$A1_copartisan[df$P_ASSIGN1 == 7 & df$party == 0] <- 1
df$A1_copartisan[df$P_ASSIGN1 == 6 & df$party == 0] <- 2 # cross-partisan
df$A1_copartisan[df$P_ASSIGN1 == 7 & df$party == 2] <- 2
df$A1_copartisan[df$party == 1] <- NA

df$A1_copartisanc <- NA
df$A1_copartisanc[df$P_ASSIGN1 == 1] <- 0 # control
df$A1_copartisanc[df$P_ASSIGN1 == 6 & df$party == 2] <- 1 # co-partisan
df$A1_copartisanc[df$P_ASSIGN1 == 7 & df$party == 0] <- 1
df$A1_copartisanc[df$party == 1] <- NA

df$A1_copartisanx <- NA
df$A1_copartisanx[df$P_ASSIGN1 == 1] <- 0 # control
df$A1_copartisanx[df$P_ASSIGN1 == 6 & df$party == 0] <- 1 # cross-partisan
df$A1_copartisanx[df$P_ASSIGN1 == 7 & df$party == 2] <- 1
df$A1_copartisanx[df$party == 1] <- NA

#### SAVE CLEAN DATA ####
save(df, file = "~/Dropbox/Public_Conf_Mil/Data_and_code/wave2_data/clean_dataw2.RData")
